package fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.travel.R;
import com.travel.find.findadapter.FindFragmentAdapter;
import com.travel.find.findbean.FindTablayoutBean;
import com.travel.find.findconstant.FindConstant;
import com.travel.find.findfragment.BournFragment;
import com.travel.find.findfragment.NewestFragment;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import fragments.shopactivity.SearchActivity;
import tools.Okhttputils;

/**
 * Created by Administrator on 2016/10/17.
 */
public class DiscoverFragment extends Fragment {
    @Bind(R.id.find_toolbar)
    Toolbar findToolbar;
    @Bind(R.id.find_tablayout)
    TabLayout findTablayout;
    @Bind(R.id.find_viewpager)
    ViewPager findViewpager;
    //fragment集合
    private List<Fragment> fragments;
    //fragmentadapter
    private FindFragmentAdapter fragmentAdapter;
    private View mView;
    //解析json获取的tagsBean集合
    private List<FindTablayoutBean.DataBean.TagsBean> tagsBeenList;
    //发送到handler的tablayoutbean集合
    private List<FindTablayoutBean.DataBean.TagsBean> handlerTablayoutBean;
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //tablayout数据
                case 0:
                    handlerTablayoutBean = (List<FindTablayoutBean.DataBean.TagsBean>) msg.obj;
                    if (handlerTablayoutBean.size() != 0) {
                        for (int i = 0; i < handlerTablayoutBean.size(); i++) {
                            if (i == 0) {
                                findTablayout.addTab(findTablayout.newTab().setText(handlerTablayoutBean.get(0).getName()), true);
                            } else {
                                findTablayout.addTab(findTablayout.newTab().setText(handlerTablayoutBean.get(i).getName()), false);
                                findTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                            }
                        }
                    }
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_discover, container, false);
        ButterKnife.bind(this, mView);
        //初始化数据
        initdata();
        //tablayout与viewpager联动
        ganged();
        initviewpager();
        //tiaozhuan     sousu
        findToolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SearchActivity.class);
                getContext().startActivity(intent);
            }
        });
        return mView;
    }
    //tablayout与viewpager联动
    private void ganged(){
        // 添加viewpager的监听 参数传递tabLayout中提供的监听事件  viewpager中的page页与tab对应
        findViewpager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(findTablayout));
        //设置tabLayout的监听事件  滑动改变tabLayout中的tab时viewpager跟随变化
        findTablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                findViewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }

        });
    }
    //初始化数据
    private void initdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String json = Okhttputils.getInstance().run(FindConstant.TABLAYOUT_VIEWPAGER);
                    String data = JSONObject.parseObject(json.trim()).getString("data");
                    String tags = JSONObject.parseObject(data).getString("tags");
                    //获取tablayoutbean
                    tagsBeenList = JSONArray.parseArray(tags.trim(), FindTablayoutBean.DataBean.TagsBean.class);
                    Message msg = Message.obtain();
                    msg.what = 0;
                    msg.obj = tagsBeenList;
                    mHandler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    //初始化适配viewpager的数据
    public void initviewpager(){
        fragments = new ArrayList<>();
        NewestFragment f1 = new NewestFragment();
        Bundle bundle=new Bundle();
        bundle.putInt("index",0);
        f1.setArguments(bundle);

        BournFragment f2 = new BournFragment();

        NewestFragment f3 = new NewestFragment();
        bundle=new Bundle();
        bundle.putInt("index",2);
        f3.setArguments(bundle);

        NewestFragment f4 = new NewestFragment();
        bundle=new Bundle();
        bundle.putInt("index",3);
        f4.setArguments(bundle);

        NewestFragment f5 = new NewestFragment();
        bundle=new Bundle();
        bundle.putInt("index",4);
        f5.setArguments(bundle);

        NewestFragment f6 = new NewestFragment();
        bundle=new Bundle();
        bundle.putInt("index",5);
        f6.setArguments(bundle);

        fragments.add(f1);
        fragments.add(f2);
        fragments.add(f3);
        fragments.add(f4);
        fragments.add(f5);
        fragments.add(f6);
        fragmentAdapter = new FindFragmentAdapter(getFragmentManager(), fragments);
        findViewpager.setAdapter(fragmentAdapter);

    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
